
cd 'D:\data_replication'

clear
clc

% Import income data for each good and market
%==========================================================================

load('estimation/5_supply_side/main_data_supply.mat');


% Goods 1-1000
%==========================================================================

Y_data_1 = [];

for i_k = 1:1000

i_k

k = index_price_coeff(i_k,1);    

data_k = data_summary{k};
Y_k = Y_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);

prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

% Prepare Data for Output Files and Save 
%--------------------------------------------------------------------------

year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
end

Y_data_temp = [i_k*ones(T,1), year_t, quarter_t, declarant_t, Y_k];
Y_data_1 = [Y_data_1; Y_data_temp];

clearvars -except Y_data_1 data_summary FE_summary income_mu_sigma index_price_coeff K K_A KK objective_MPEC_summary price_coeff_MPEC price_table status_MPEC_index status_MPEC_summary v_summary X_MPEC_opt_summary X_MPEC_summary Y_summary  

end

save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_1.mat', 'Y_data_1');  


% Goods 1001-2000
%==========================================================================

Y_data_2 = [];

for i_k = 1001:2000

i_k

k = index_price_coeff(i_k,1);    

data_k = data_summary{k};
Y_k = Y_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);

prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

% Prepare Data for Output Files and Save 
%--------------------------------------------------------------------------

year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
end

Y_data_temp = [i_k*ones(T,1), year_t, quarter_t, declarant_t, Y_k];
Y_data_2 = [Y_data_2; Y_data_temp];

clearvars -except Y_data_2 data_summary FE_summary income_mu_sigma index_price_coeff K K_A KK objective_MPEC_summary price_coeff_MPEC price_table status_MPEC_index status_MPEC_summary v_summary X_MPEC_opt_summary X_MPEC_summary Y_summary  

end

save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_2.mat', 'Y_data_2');  


% Goods 2001-3000
%==========================================================================

Y_data_3 = [];

for i_k = 2001:3000

i_k

k = index_price_coeff(i_k,1);    

data_k = data_summary{k};
Y_k = Y_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);

prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

% Prepare Data for Output Files and Save 
%--------------------------------------------------------------------------

year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
end

Y_data_temp = [i_k*ones(T,1), year_t, quarter_t, declarant_t, Y_k];
Y_data_3 = [Y_data_3; Y_data_temp];

clearvars -except Y_data_3 data_summary FE_summary income_mu_sigma index_price_coeff K K_A KK objective_MPEC_summary price_coeff_MPEC price_table status_MPEC_index status_MPEC_summary v_summary X_MPEC_opt_summary X_MPEC_summary Y_summary  

end

save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_3.mat', 'Y_data_3');  


% Goods 3001-4092
%==========================================================================

Y_data_4 = [];

for i_k = 3001:4092

i_k

k = index_price_coeff(i_k,1);    

data_k = data_summary{k};
Y_k = Y_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);

prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

% Prepare Data for Output Files and Save 
%--------------------------------------------------------------------------

year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
end

Y_data_temp = [i_k*ones(T,1), year_t, quarter_t, declarant_t, Y_k];
Y_data_4 = [Y_data_4; Y_data_temp];

clearvars -except Y_data_4 data_summary FE_summary income_mu_sigma index_price_coeff K K_A KK objective_MPEC_summary price_coeff_MPEC price_table status_MPEC_index status_MPEC_summary v_summary X_MPEC_opt_summary X_MPEC_summary Y_summary  

end

save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_4.mat', 'Y_data_4');  


% Merge Files
%==========================================================================

load('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_1.mat');
load('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_2.mat');
load('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_3.mat');
load('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_4.mat');

Y_i_summary = [Y_data_1; Y_data_2; Y_data_3; Y_data_4]; 
save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_i_summary.mat', 'Y_i_summary');

data_save = array2table(Y_i_summary);
data_save.Properties.VariableNames(1:4) = {'i_k', 'year', 'quarter' 'declarant'};  
writetable(data_save, 'estimation/4_demand_estimation/4_cobb_douglas_weights/Y_i_summary.csv', 'Delimiter',',','QuoteStrings',true);

% Clean up 
%--------------------------------------------------------------------------

delete 'estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_1.mat'
delete 'estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_2.mat'
delete 'estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_3.mat'
delete 'estimation/4_demand_estimation/4_cobb_douglas_weights/Y_data_4.mat'


